package com.gzdh.dishrecognition.ui.fragment.sysinfo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dhkj.web.bean.MacMainpFoodMenu;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.adapter.FoodsCardSetAdapter;
import com.gzdh.dishrecognition.ui.dialog.FoodsEditFragment;
import com.gzdh.dishrecognition.ui.dialog.identify.LoginFragment;
import com.gzdh.dishrecognition.ui.widget.SwipeLayoutManager;

import java.util.List;

public class FoodCardSetFragment extends Fragment {
    private Activity activity;
    private Context mContext;


    private FoodsCardSetAdapter foodsCardSetAdapter;

    public FoodCardSetFragment(Activity activity) {
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = inflater.getContext();
        foodsCardSetAdapter = new FoodsCardSetAdapter(mContext, getChildFragmentManager());
        FoodsCardSetAdapter.OnClick onClick = new FoodsCardSetAdapter.OnClick() {
            @Override
            public void makeClick(MacMainpFoodMenu macMainpFoodMenu) {
                //制卡呼唤起

            }
        };

        foodsCardSetAdapter.setOnClick(onClick);
        View view = getViews(inflater);
        return view;
    }

    private View getViews(LayoutInflater inflater) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout mainLayout = new LinearLayout(inflater.getContext());
        mainLayout.setLayoutParams(layoutParams);
        mainLayout.setBackgroundColor(Color.WHITE);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.addView(getTitleView());   //标题初始化
        mainLayout.addView(getBody());        //主题内容
        return mainLayout;
    }


    private View getTitleView() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout titleLinear = new LinearLayout(mContext);
        titleLinear.setLayoutParams(layoutParams);
        titleLinear.setBackgroundColor(Color.WHITE);
        titleLinear.setOrientation(LinearLayout.VERTICAL);

        RelativeLayout titleRv = new RelativeLayout(mContext);
        titleRv.setLayoutParams(layoutParams);
        titleLinear.addView(titleRv);

        //-------------------------标题
        TextView titleTv = new TextView(mContext);
        titleTv.setText("菜  卡  查  询 ");
        RelativeLayout.LayoutParams tt1 = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        int tt1Margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, this.getResources().getDisplayMetrics());
        tt1.setMargins(tt1Margins, tt1Margins, tt1Margins, tt1Margins);
        tt1.addRule(RelativeLayout.CENTER_IN_PARENT);
        titleTv.setLayoutParams(tt1);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        titleTv.setTextColor(Color.BLACK);
        titleRv.addView(titleTv);

        //--------------------------右侧新增按钮
        TextView addBut = new TextView(mContext);
        addBut.setVisibility(View.GONE);
        RelativeLayout.LayoutParams addButParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        int addButParamsMargins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, this.getResources().getDisplayMetrics());
        addButParams.setMargins(0, 0, addButParamsMargins, 0);
        addButParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        addButParams.addRule(RelativeLayout.CENTER_VERTICAL);
        addBut.setText(getResources().getString(R.string.add));
        addBut.setLayoutParams(addButParams);
        addBut.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
        addBut.setTextColor(Color.WHITE);
        addBut.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        addBut.setPadding(15, 0, 15, 0);
        titleRv.addView(addBut);

        addBut.setOnClickListener(view -> {
            FoodsEditFragment foodsEditFragment = new FoodsEditFragment();
            foodsEditFragment.show(getChildFragmentManager(), LoginFragment.class.getSimpleName());
        });

        //----------------------------下划线
        View view = new View(mContext);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        view.setBackgroundColor(Color.rgb(224, 224, 224));
        titleLinear.addView(view);

        return titleLinear;
    }

    private View getBody() {
        LinearLayout mainLinear = new LinearLayout(mContext);
        mainLinear.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        mainLinear.setBackground(getResources().getDrawable(R.drawable.background_w));
        mainLinear.setOrientation(LinearLayout.VERTICAL);

        LinearLayout topLinear = new LinearLayout(mContext);
        topLinear.setOrientation(LinearLayout.HORIZONTAL);
        topLinear.setBackgroundColor(Color.rgb(240, 240, 240));
        topLinear.setPadding(0, 15, 0, 15);
        topLinear.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        mainLinear.addView(topLinear);

        LinearLayout.LayoutParams topLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 10);

        TextView xuhaoTopTv = new TextView(mContext);
        xuhaoTopTv.setText("序号");
        xuhaoTopTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        xuhaoTopTv.setTextColor(Color.BLACK);
        xuhaoTopTv.setLayoutParams(topLayoutParams);
        xuhaoTopTv.setGravity(Gravity.CENTER);
        topLinear.addView(xuhaoTopTv);

        TextView cardNumTv = new TextView(mContext);
        cardNumTv.setText("卡号");
        cardNumTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        cardNumTv.setTextColor(Color.BLACK);
        cardNumTv.setLayoutParams(topLayoutParams);
        cardNumTv.setGravity(Gravity.CENTER);
        topLinear.addView(cardNumTv);

        TextView bmTopTv = new TextView(mContext);
        bmTopTv.setText("菜名");
        bmTopTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        bmTopTv.setTextColor(Color.BLACK);
        bmTopTv.setLayoutParams(topLayoutParams);
        bmTopTv.setGravity(Gravity.CENTER);
        topLinear.addView(bmTopTv);

        TextView nameTopTv = new TextView(mContext);
        nameTopTv.setText("价格");
        nameTopTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        nameTopTv.setTextColor(Color.BLACK);
        nameTopTv.setLayoutParams(topLayoutParams);
        nameTopTv.setGravity(Gravity.CENTER);
        topLinear.addView(nameTopTv);

        TextView caozuoTv = new TextView(mContext);
        caozuoTv.setVisibility(View.GONE);
        caozuoTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        caozuoTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 11));
        caozuoTv.setGravity(Gravity.CENTER);
        topLinear.addView(caozuoTv);
        caozuoTv = new TextView(mContext);
        caozuoTv.setVisibility(View.GONE);
        caozuoTv.setText("操作");
        caozuoTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        caozuoTv.setTextColor(Color.BLACK);
        caozuoTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 11));
        caozuoTv.setGravity(Gravity.CENTER);
        topLinear.addView(caozuoTv);
        caozuoTv = new TextView(mContext);
        caozuoTv.setVisibility(View.GONE);
        caozuoTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        caozuoTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 11));
        caozuoTv.setGravity(Gravity.CENTER);
        topLinear.addView(caozuoTv);

        RecyclerView recyclerView = new RecyclerView(mContext);
        recyclerView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager2);
        recyclerView.setAdapter(foodsCardSetAdapter);


        mainLinear.addView(recyclerView);
        return mainLinear;
    }

    @Override
    public void onResume() {
        super.onResume();
        RequestUtils.getFoodsMenuList(mContext, new MyObserverRes<List<MacMainpFoodMenu>>(mContext) {
            @Override
            public void onSuccess(List<MacMainpFoodMenu> result) {
                foodsCardSetAdapter.setList(result);

            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {

            }
        });
    }

    @Override
    public void onPause() {
        SwipeLayoutManager.getInstance().clearCurrentLayout();
        super.onPause();
    }

}
